var MainJS = "CLodopfuncs.js",
  URL_WS1 = "ws://localhost:8000/" + MainJS,
  URL_WS2 = "ws://localhost:18000/" + MainJS,
  URL_HTTP1 = "http://localhost:8000/" + MainJS,
  URL_HTTP2 = "http://localhost:18000/" + MainJS,
  URL_HTTP3 = "https://localhost.lodop.net:8443/" + MainJS,
  LoadJsState;

function getCLodopJsState() {
  return LoadJsState;
}

function checkOrTryHttp() {
  if (window.getCLodop) return (LoadJsState = "complete"), !0;
  if ("loadingB" != LoadJsState && "complete" != LoadJsState) {
    LoadJsState = "loadingB";
    var t =
        document.head ||
        document.getElementsByTagName("head")[0] ||
        document.documentElement,
      e = document.createElement("script"),
      n = document.createElement("script"),
      r = document.createElement("script");
    (e.onload =
      n.onload =
      r.onload =
      n.onerror =
      r.onerror =
        function () {
          LoadJsState = "complete";
        }),
      (e.onerror = function (e) {
        (n.src = URL_HTTP2), t.insertBefore(n, t.firstChild);
      }),
      "https:" !== window.location.protocol
        ? ((e.src = URL_HTTP1), t.insertBefore(e, t.firstChild))
        : ((r.src = URL_HTTP3), t.insertBefore(r, t.firstChild));
  }
}

export function getLodop() {
  var t,
    e = "https://scp-tcdn.sf-express.com/scp/soft/SCPPrint_Win32NT_6.570CN.exe",
    n =
      "<br><div class='scpprint-tips'>Web打印服务CLodop未安装启动，点击这里<a href=".concat(
        e,
        " target='_self'>下载执行安装</a>"
      ),
    r =
      "<br><div class='scpprint-tips'>Web打印服务CLodop需升级!点击这里<a href=".concat(
        e,
        " target='_self'>执行升级</a>"
      ),
    o = "，成功后请刷新本页面或重启浏览器。</div>";
  try {
    t = window.getCLodop();
  } catch (t) {}
  return t || "complete" === LoadJsState
    ? t
      ? CLODOP.CVERSION < "4.1.6.0"
        ? {
            code: 3,
            msg: r + o,
            downloadUrl: e,
          }
        : (t.SET_LICENSES(
            "顺丰科技有限公司",
            "A8014B09DC3900222D3047E9942A8F3504D",
            "順豐科技有限公司",
            "EA15AFAF29B939797009E405CDEB043768A"
          ),
          t.SET_LICENSES(
            "THIRD LICENSE",
            "",
            "Sf Technology Co., Ltd.",
            "F5BD5E2D3083D6F7FA2FF6C5DFEB3740F52"
          ),
          t)
      : {
          code: 2,
          msg:
            n +
            "<br>（若此前已安装过，可<a href='CLodop.protocol:setup' target='_self'>点这里直接再次启动</a>）" +
            o +
            "<br>注：内网用户可通过顺丰自主工具一键安装【顺丰云打印插件】</br>",
          downloadUrl: e,
        }
    : {
        code: 4,
        msg: "网页还没下载完毕，请稍等一下再操作。",
      };
}

export function loadCLodop() {
  (LoadJsState = "loadingA"),
    !window.WebSocket &&
      window.MozWebSocket &&
      (window.WebSocket = window.MozWebSocket);
  try {
    var WSK1 = new WebSocket(URL_WS1);
    (WSK1.onopen = function (t) {
      setTimeout(function () {
        checkOrTryHttp();
      }, 200);
    }),
      (WSK1.onmessage = function (e) {
        window.getCLodop || eval(e.data);
      }),
      (WSK1.onerror = function (e) {
        var WSK2 = new WebSocket(URL_WS2);
        (WSK2.onopen = function (t) {
          setTimeout(function () {
            checkOrTryHttp();
          }, 200);
        }),
          (WSK2.onmessage = function (e) {
            window.getCLodop || eval(e.data);
          }),
          (WSK2.onerror = function (t) {
            checkOrTryHttp();
          });
      });
  } catch (t) {
    checkOrTryHttp();
  }
}
